<?php
/**
 * @description : request a unauthorized Request Token
 * @author : stephen
 * @date : 2010/07/07
 */
ini_set('display_errors','on');
require_once "TVB/MSApi/Client/requires.php";
require_once "TVB/mytvplus/portal_service/client/requires.php";
require_once "TVB/OAuth/common.inc.php";

try {
    $req = OAuthRequest::from_request();
    list($consumer, $token) = $tvb_server->verify_request($req);
        
    $request = $req->get_parameters();
    //get api list or value.
    $func_name = $request['oauth_apiname'];
    $consumer_key = $request['oauth_consumer_key'];
    $oauth_token = $request['oauth_token'];

    //Its Testing Codes.
    $authorized_data = OAuthModel::getInstance()->getAuthorizedData($oauth_token);
    if (empty($authorized_data)) {
        throw new OAuthException('Expired Token "'.$oauth_token.'"');
        #echo OAuthConfig::OAUTH_TOKEN_EXPIRED;
        exit;
    }
    
    $data = unserialize($authorized_data);
    $datas = array('req'=>$request,'db'=>$data);

    //call this define API
    $result = OAuthModel::getInstance()->getOAuthApis($func_name,$datas);
    if ($result===FALSE) {
        throw new OAuthException('API '.$func_name.' has some wrong!');
        #echo OAuthConfig::CALL_OAUTH_APIS_FAIL;
        exit;
    }

    if ($result == -2) {
        throw new OAuthException('Expired Token "'.$oauth_token.'"');
        exit;
    }
    
    print $result;
    exit;
}
catch (OAuthException $e) {
    print($e->getMessage() . "\n<hr />\n");
    print_r($req);
    #echo OAuthConfig::OTHER_OAUTH_ERROR;
    exit;
}

?>
